package xipujewel.xipujewel.service;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.Map;

import org.apache.commons.io.FileUtils;
import org.hibernate.Session;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import xipujewel.xipujewel.entity.app.Ring;
import xipujewel.xipujewel.repository.jpa.RingDao;
import xipujewel.xipujewel.service.common.JpaManager;

@Component
@Transactional
public class RingManager extends JpaManager<Ring, RingDao> {

	private static Logger logger = LoggerFactory.getLogger(RingManager.class);

	@Autowired
	private RingDao ringDao;

	public void save(Map<String, MultipartFile> fileMap, Ring ring, String serverPath) throws IOException {
		MultipartFile file = fileMap.get("file");
		MultipartFile file1 = fileMap.get("file1");
		MultipartFile file2 = fileMap.get("file2");
		MultipartFile file3 = fileMap.get("file3");
		MultipartFile file4 = fileMap.get("file4");
		String fileName = "";
		String fileName1 = "";
		String fileName2 = "";
		String fileName3 = "";
		String fileName4 = "";
		if (ring.getId() != null && ring.getId() != 0) {
			if (file != null && file.getSize() != 0) {
				new File(serverPath + ring.getPhoto()).delete();
				fileName = "/photo/" + new Date().getTime() + "0"
						+ file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
				FileUtils.writeByteArrayToFile(new File(serverPath + fileName), file.getBytes());
				ring.setPhoto(fileName);
			}
			if (file1 != null && file1.getSize() != 0) {
				new File(serverPath + ring.getPhoto1()).delete();
				fileName1 = "/photo/" + new Date().getTime() + "1"
						+ file1.getOriginalFilename().substring(file1.getOriginalFilename().lastIndexOf("."));
				FileUtils.writeByteArrayToFile(new File(serverPath + fileName1), file1.getBytes());
				ring.setPhoto1(fileName1);
			}
			if (file2 != null && file2.getSize() != 0) {
				new File(serverPath + ring.getPhoto2()).delete();
				fileName2 = "/photo/" + new Date().getTime() + "2"
						+ file2.getOriginalFilename().substring(file2.getOriginalFilename().lastIndexOf("."));
				FileUtils.writeByteArrayToFile(new File(serverPath + fileName2), file2.getBytes());
				ring.setPhoto2(fileName2);
			}
			if (file3 != null && file3.getSize() != 0) {
				new File(serverPath + ring.getPhoto3()).delete();
				fileName3 = "/photo/" + new Date().getTime() + "3"
						+ file3.getOriginalFilename().substring(file3.getOriginalFilename().lastIndexOf("."));
				FileUtils.writeByteArrayToFile(new File(serverPath + fileName3), file3.getBytes());
				ring.setPhoto3(fileName3);
			}
			if (file4 != null && file4.getSize() != 0) {
				new File(serverPath + ring.getPhoto4()).delete();
				fileName4 = "/photo/" + new Date().getTime() + "4"
						+ file4.getOriginalFilename().substring(file4.getOriginalFilename().lastIndexOf("."));
				FileUtils.writeByteArrayToFile(new File(serverPath + fileName4), file4.getBytes());
				ring.setPhoto4(fileName4);
			}
		} else {
			if (file != null && file.getSize() != 0) {
				fileName = "/photo/" + new Date().getTime() + "0"
						+ file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
				FileUtils.writeByteArrayToFile(new File(serverPath + fileName), file.getBytes());
				ring.setPhoto(fileName);
			}
			if (file1 != null && file1.getSize() != 0) {
				fileName1 = "/photo/" + new Date().getTime() + "1"
						+ file1.getOriginalFilename().substring(file1.getOriginalFilename().lastIndexOf("."));
				FileUtils.writeByteArrayToFile(new File(serverPath + fileName1), file1.getBytes());
				ring.setPhoto1(fileName1);
			}
			if (file2 != null && file2.getSize() != 0) {
				fileName2 = "/photo/" + new Date().getTime() + "2"
						+ file2.getOriginalFilename().substring(file2.getOriginalFilename().lastIndexOf("."));
				FileUtils.writeByteArrayToFile(new File(serverPath + fileName2), file2.getBytes());
				ring.setPhoto2(fileName2);
			}
			if (file3 != null && file3.getSize() != 0) {
				fileName3 = "/photo/" + new Date().getTime() + "3"
						+ file3.getOriginalFilename().substring(file3.getOriginalFilename().lastIndexOf("."));
				FileUtils.writeByteArrayToFile(new File(serverPath + fileName3), file3.getBytes());
				ring.setPhoto3(fileName3);
			}
			if (file4 != null && file4.getSize() != 0) {
				fileName4 = "/photo/" + new Date().getTime() + "4"
						+ file4.getOriginalFilename().substring(file4.getOriginalFilename().lastIndexOf("."));
				FileUtils.writeByteArrayToFile(new File(serverPath + fileName4), file4.getBytes());
				ring.setPhoto4(fileName4);
			}
		}
		save(ring);
	}

	@Transactional(readOnly = false)
	public void delete(Long id, String serverPath) {
		Session session = (Session) entityManager.getDelegate();
		Ring ring = ringDao.findOne(id);
		deletePicture(ring, serverPath);
		String deleteOrderRing = "delete from order_ring  where ring_id=" + id;
		session.createSQLQuery(deleteOrderRing).executeUpdate();
		String deleteShopCart = "delete from xipu_shopping_cart  where ring_id=" + id;
		session.createSQLQuery(deleteShopCart).executeUpdate();
		ringDao.delete(id);
	}

	public void deletePicture(Ring ring, String serverPath) {
		if (ring.getPhoto() != null) {
			new File(serverPath + ring.getPhoto()).delete();
		}
		if (ring.getPhoto1() != null) {
			new File(serverPath + ring.getPhoto1()).delete();
		}
		if (ring.getPhoto2() != null) {
			new File(serverPath + ring.getPhoto2()).delete();
		}
		if (ring.getPhoto3() != null) {
			new File(serverPath + ring.getPhoto3()).delete();
		}
		if (ring.getPhoto4() != null) {
			new File(serverPath + ring.getPhoto4()).delete();
		}
	}

	@Override
	protected RingDao getDao() {
		return ringDao;
	}

}
